﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace carApp
{
    public partial class GoodsSelect : Form
    {
        public GoodsSelect()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void GoodsSelect_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            LoadData();
        }
        #region pager
        int pageSize = 20;
        int CurrentPage = 1;
        int pageCount = 0;
        DataTable dt;
        int CurrentRowIndex = 0;
        int CurrentColumnIndex = 0;
        void LoadData()
        {
            string where = "";
            if (txtKeyword.TextLength > 0)
                where = " name like'%" + txtKeyword.Text + "%'";
            int RowCount = db.count("goods", where);
            dt = db.GetPagerData("goods", "*", where, pageSize, CurrentPage);
            pageCount = (int)Math.Ceiling(RowCount * 1.0 / pageSize);
            lblPageCount.Text = "共 " + pageCount + " 页";
            txtPageCurrent.Text = CurrentPage.ToString();
            dataGridView1.DataSource = dt;
        }
        private void lblfirst_Click(object sender, EventArgs e)
        {
            CurrentPage = 1;
            LoadData();
        }

        private void lblprev_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            if (CurrentPage < 1)
                CurrentPage = 1;
            LoadData();
        }

        private void lblNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            if (CurrentPage > pageCount)
                CurrentPage = pageCount;
            LoadData();
        }

        private void lbllast_Click(object sender, EventArgs e)
        {
            CurrentPage = pageCount;
            LoadData();
        }
        private void btnPageGo_Click(object sender, EventArgs e)
        {
            CurrentPage = int.Parse(txtPageCurrent.Text);
            LoadData();
        }
        #endregion

        private void button6_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        private List<string> ids =new List<string>();
        private void button2_Click_1(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    ids.Add(dataGridView1.Rows[i].Cells[1].Value.ToString());
                }
            }
            if (ids.Count == 0)
            {
                MessageBox.Show("请选择商品");
            }
            else
            {
                Ticket ower = (Ticket)this.Owner;
                string goodsId = string.Join(",", ids.ToArray());
                ower.addGoodsToDataView(goodsId);
                this.Close();
            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
                if ((bool)dataGridView1.Rows[e.RowIndex].Cells[0].EditedFormattedValue == true)
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = false;
                }
                else
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = true;
                }
            }
        }
    }
}
